//
//  CategoryCellChildView.m
//  JiaoX
//
//  Created by 尹桥印 on 14/11/18.
//  Copyright (c) 2014年 北京娇羞科技有限公司. All rights reserved.
//  分类里面每一个小条目，包含一个图片，一个标题

#import "CategoryCellChildView.h"
#import "UIImageView+WebCache.h"





@interface CategoryCellChildView()

@property (nonatomic ,weak) UIImageView *icon;
@property (nonatomic ,weak) UILabel *title;
@property (nonatomic ,weak) UIButton *button;
@property (nonatomic ,strong) SubCategory *sub;

@end


@implementation CategoryCellChildView

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self!=nil){
        
        // 1.添加图标view
        UIImageView *icon = [[UIImageView alloc]init];
        
        //设置layer
        CALayer *layer=[icon layer];
        //是否设置边框以及是否可见
        [layer setMasksToBounds:YES];
        //设置边框线的宽
        [layer setBorderWidth:0.5];
        //设置边框线的颜色
        [layer setBorderColor:[JXColor(242, 242, 242) CGColor]];
        
        icon.width = CATEGORY_CHILDVIEW_HEIGHT;
        icon.height = CATEGORY_CHILDVIEW_HEIGHT;
        icon.x = 0;
        icon.y = 0;
        _icon = icon;
        [self addSubview:icon];
        
        // 2.添加label
        UILabel *title = [[UILabel alloc]init];
        title.textAlignment = NSTextAlignmentCenter;
        title.font = [UIFont systemFontOfSize:TITLE_FONT_SIZE];
        title.x = 0;
        title.y = icon.height+5;
        title.textColor = [UIColor blackColor];
        title.width = icon.width;
        title.height = TITLE_FONT_SIZE;
        _title = title;
        [self addSubview:title];
        self.bounds = CGRectMake(0, 0, icon.width, CGRectGetMaxY(title.frame));
        
        
        //添加一个btn，用于点击效果
        UIButton *button = [[UIButton alloc]init];
        [button setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageWithColor:JXColorAlpha(204, 204, 204,150)] forState:UIControlStateHighlighted];
        button.frame = CGRectMake(0, 0, self.width, self.height);
        [button addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        _button = button;
        [self insertSubview:button aboveSubview:icon];
        
    }
    return self;
}

-(void)setSub:(SubCategory *)sub{
    _sub = sub;
    self.title.text = sub.categoryName;
    [self.icon sd_setImageWithURL:[NSURL URLWithString:sub.categoryIcon]];
}

-(void)btnClick{
    // 通知代理
    if ([self.itemClickDelegate respondsToSelector:@selector(subItemClickDelegate:)]) {
        [self.itemClickDelegate subItemClickDelegate:self.sub];
    }
    
}

+(instancetype)initWithIconSubCategory:(SubCategory *)sub{
    CategoryCellChildView *view = [[self alloc]init];
    view.sub = sub;
    [view.icon sd_setImageWithURL:[NSURL URLWithString:sub.categoryIcon]];
    view.title.text = sub.categoryName;
    return view;
}

@end
